"""
Case Type   : 兼容M*数据库datetime数据类型
Case Name   : YYYYMMDDhhmmss or YYMMDDhhmmss格式，显示转换
Create At   : 2023.10.07
Owner       : z@wan005
Description :
    1、创建数据库
    2、使用::转换为datetime类型
    3、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
History     :sql用例无法返回'0000-00-00 00:00:00'，修改用例为py
              --modified by zhanghuan96，2024.06.28，优化断言
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest0025(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = "db_datetime_0025"

    def test_datetime(self):
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        text = '--step1:创建数据库;expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility 'b';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res)

        text = '--step2:使用::转换为datetime类型;expect:成功--'
        self.log.info(text)
        sql_cmd = f"select 11::datetime;" \
                  f"select 111::datetime;" \
                  f"select 1111::datetime;" \
                  f"select 11111::datetime;" \
                  f"select 111111::datetime;" \
                  f"select 1111111::datetime;" \
                  f"select 11111111::datetime;" \
                  f"select 111111111::datetime;" \
                  f"select 1111111111::datetime;" \
                  f"select 11111111111::datetime;" \
                  f"select 111111111111::datetime;" \
                  f"select 1111111111111::datetime;" \
                  f"select 11111111111111::datetime;"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd,
                                                dbname=self.db_name)
        self.log.info(sql_res)
        res_list = ['', '2000-01-11 00:00:00',
                    '2000-11-11 00:00:00', '2001-11-11 00:00:00',
                    '2011-11-11 00:00:00', '0111-11-11 00:00:00',
                    '1111-11-11 00:00:00', '2000-01-11 11:11:11',
                    '2000-11-11 11:11:11', '2001-11-11 11:11:11',
                    '2011-11-11 11:11:11', '0111-11-11 11:11:11',
                    '1111-11-11 11:11:11']
        for res in res_list:
            self.assertIn(res, sql_res, '执行失败:' + text)

    def tearDown(self):
        text = '----step3:清理环境;expect:成功----'
        self.log.info(text)
        sql_cmd = f"drop database {self.db_name};"
        self.log.info(sql_cmd)
        drop_res1 = self.sh_primary.execut_db_sql(sql_cmd,
                                                  dbname='postgres')
        self.log.info(drop_res1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, drop_res1,
                      '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
